/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.levels;

import com.jme3.app.state.AbstractAppState;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import exterminator.Main;
import exterminator.game.Game.Lost;
import exterminator.levels.Deck.Bug;
import exterminator.levels.Deck.Muck;
import tonegod.gui.core.Screen;
import tonegod.gui.core.SubScreen;

/**
 *
 * @author t0neg0d
 */
public class Levels extends AbstractAppState {
	private Main main;
	private Screen screen;
	private Node[] deckNodes = new Node[4];
	private Deck[] decks = new Deck[4];
	private Vector2f[] dims = new Vector2f[4];
	private SubScreen[] deckSubScreens = new SubScreen[4];
	private int deckIndex = 0;
	
	public int maxMuckLife = 20;
	private float muckTime = 1f;
	private float currentMuckTime = 0;
	private boolean runMuckCheck = false;
	
	private int maxDeckBugs = 25;
	private float releaseTime = 0.5f;
	private float currentReleaseTime = 0f;
	private boolean runRelease = false;
	
	private int randIndex = 0;
	private int winTotal = 50;
	
	public Levels(Main main, int winTotal) {
		this.main = main;
		this.screen = main.getScreen();
		muckTime /= (float)maxMuckLife;
		this.winTotal = winTotal;
	}
	
	public int getWinTotal() {
		return this.winTotal;
	}
	
	public void addDeck(Geometry geom, Vector2f dim, int maxbugs) {
		SubScreen subScreen = new SubScreen(screen, geom);
		screen.addSubScreen(subScreen);
		dims[deckIndex] = dim;
		maxDeckBugs = maxbugs;
		
		Deck deck = new Deck(main, this, deckIndex);
		decks[deckIndex] = deck;
		
		Node deckNode = new Node();
		main.getRootNode().attachChild(subScreen.getGeometry());
		subScreen.setSubScreenBridge((int)dim.x, (int)dim.y, deckNode);
		subScreen.getSubScreenBridge().setViewPortColor(new ColorRGBA(1,1,1,0));
		deckNodes[deckIndex] = deckNode;
		deckSubScreens[deckIndex] = subScreen;
		subScreen.getMaterial().getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
		
		deck.populateDeck();
		
		deckIndex++;
	}
	
	public SubScreen getDeckSubScreen(int index) {
		return deckSubScreens[index];
	}
	
	public Vector2f getDeckDimensions(int index) {
		return dims[index];
	}
	
	public Node getDeckNode(int index) {
		return deckNodes[index];
	}
	
	public void reset() {
		for (Deck deck : decks) {
			if (deck != null) {
				for (Bug bug : deck.getBugs()) {
					deck.getElBugs().removeChild(bug);
				}
				deck.clearBugs();
				for (Muck muck : deck.getMucks()) {
					deck.getElMucks().removeChild(muck);
				}
				deck.clearMuck();
				deck.bugCount = 0;
			}
		}
		currentMuckTime = 0;
		currentReleaseTime = 0;
		runMuckCheck = false;
		runRelease = false;
	}
	
	@Override
	public void update(float tpf) {
		if (main.getGame().getIsGameActive()) {
			currentReleaseTime += tpf;
			if (currentReleaseTime >= releaseTime) {
				runRelease = true;
				randIndex = FastMath.nextRandomInt(0, 3);
			}

			for (Deck deck : decks) {
				if (deck != null) {
					if (deck.getBugCount() >= maxDeckBugs) {
						main.endGame(Lost.Overrun);
					}
					if (runRelease && randIndex == deck.deckIndex) {
						if (deck.getBugCount() < maxDeckBugs) {
							if (FastMath.nextRandomFloat() > .75f)
								deck.releaseBug();
						}
					}
					for (Bug bug : deck.getBugs()) {
						if (!bug.getIsAlive()) {
							deck.getSubScreen().removeElement(bug);
							deck.removeBug(bug);
							break;
						} else {
							bug.moveBug();
						}
					}
				}
			}

			if (runRelease) {
				runRelease = false;
				currentReleaseTime = 0;
			}

			if (main.getHose().isActive() && !main.getHose().isFlaggedForRemoval()) {
				currentMuckTime += tpf;
				if (currentMuckTime >= muckTime) {
					runMuckCheck = true;
					currentMuckTime -= muckTime;
				}

				if (runMuckCheck) {
					for (Deck deck : decks) {
						if (deck != null) {
							for (Muck muck : deck.getMucks()) {
								if (muck.getHoseFocus())
									muck.decLife();
							}
							for (Muck muck : deck.getMucks()) {
								if (!muck.getIsAlive()) {
									deck.getSubScreen().removeElement(muck);
									deck.removeMuck(muck);
									break;
								}
							}
						}
					}
					runMuckCheck = false;
				}
			}
		}
	}
}
